<?php
    session_start();
?>

<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
        
            <link rel="stylesheet" href="styles/layout.css" type="text/css"/>
            <link rel="stylesheet" href="styles/computer.css" type="text/css"/>
            <script type="text/javascript" src="scripts/computer.js"></script>
        
    <script type="text/javascript">
        var xmlhttp;
        var cartID;
        var i;
        
        function deleteItem(){
            
            var cartIDRB = document.shoppingCart.cartID;
            for(i = 0; i < cartIDRB.length; i++){
                if(cartIDRB[i].checked){
                    cartID = cartIDRB[i].value;
                    alert(cartID);
                    break;
                }
            }
            

            if(window.XMLHttpRequest){
                //code for IE7+, Firefox, Chrome, and Safari
                xmlhttp = new XMLHttpRequest();
            }else{
                //code for IE6 and IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }

            xmlhttp.onreadystatechange = handleReply;
            xmlhttp.open("GET", "deleteItem.php?cartID="+cartID, true);
            xmlhttp.send();            
        }

        function handleReply(){
            if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
//                    alert(xmlhttp.responseText);
                userTable = document.getElementById("cartTable");
                userTable.deleteRow(i+1);
            }
        }
    </script>
            
    </head>
    <body>
        <?php        require 'header.html';
        ?>
        
<div id="container">
<div class="wrapper">
<div id="content">
<div id="order">
        
        <?php
        // put your code here
            echo "Welcome customer ".$_SESSION["firstName"]." ".$_SESSION["lastName"];
//            echo session_id();
            
            $con = mysql_connect("localhost", "zhouzhao", "19831022");
            if(!$con){
                die("could not connect to DB: ".mysql_error());
            }
            
            mysql_select_db("cloudcom", $con);
            
            /*For up to 5 points extra credit, keep track of what items are purchased together - like what Amazon does. When a customer puts an item in their shopping cart, display item(s) that other customers also bought in a single order.*/
            
            if(isset($_SESSION["customerID"])){
                $result = mysql_query("select * from shoppingCarts S, products P where S.productID = P.productID and S.sessionID='".$_SESSION["customerID"]."'");
                $result1 = mysql_query("select * from products P where P.categoryID in (select P.categoryID from shoppingCarts S, products P where S.productID=P.productID and S.sessionID=".$_SESSION["customerID"].")");
            }else{
                $result = mysql_query("select * from shoppingCarts S, products P where S.productID = P.productID and S.sessionID='".session_id()."'");
                $result1 = mysql_query("select * from products P where P.categoryID in (select P.categoryID from shoppingCarts S, products P where S.productID=P.productID and S.sessionID=".session_id().")");
            }
        ?>
            
            <form method="post" name="shoppingCart" action="payment.php">
                <table border="1" id="cartTable">
                    <tr>
                        <th>Product Name</th>
                        <th>Product Description</th>
                        <th>Product Image</th>
                        <th>Product Price</th>
                        <th>Product Quantity</th>
                        <th>Select</th>
                    </tr>
                    <?php
                        while($row = mysql_fetch_assoc($result)){
                            echo "<tr>";
                            echo "<td>".$row["productName"]."</td>";
                            echo "<td>".$row["productDescription"]."</td>";
                            echo "<td><img src='images/products/".$row["productImage"]."' width='100' height='100' alt='image' /></td>";
                            echo "<td>".$row["productPrice"]."</td>";
                            echo "<td>".$row["productQuantity"]."</td>";
                            echo "<td><input type='radio' name='cartID' value='".$row["cartID"]."'/></td>";
                            echo "</tr>";
                        }
                        mysql_close($con);
                    ?>
                </table>
                <input type="submit" value="Checkout" class="buttonStyle" onmouseover="changeOn(this)" onmouseout="changeOff(this)"/>
                <input type="button" value="Delete Item" class="buttonStyle" onclick="deleteItem()" onmouseover="changeOn(this)" onmouseout="changeOff(this)"/>
                
            </form>
    <form method="post" name="customer" action="updateCart.php">   
        <p>Other customer also order the following items</p>
                <table border="1" id="productTable">
                    <tr>
                        <th>Category ID</th>
                        <th>Product name</th>
                        <th>Product description</th>
                        <th>Product image</th>
                        <th>Product price</th>
                        <th>Select</th>
                    </tr>
                    <?php
                        while($row1 = mysql_fetch_assoc($result1)){
                            echo "<tr>";
                            echo "<td>".$row1["categoryID"]."</td>";
                            echo "<td>".$row1["productName"]."</td>";
                            echo "<td>".$row1["productDescription"]."</td>";
                            echo "<td><img src='images/products/".$row1["productImage"]."' width='100' heigth='100' alt='image' /></td>";
                            echo "<td>".$row1["productPrice"]."</td>";
                            echo "<td><input type='radio' name='productID' value='".$row1["productID"]."'/></td>";
                            echo "</tr>";
                        }
                        
                    ?>
                </table>
        <select name="quantity">
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                    <option value="5">5+</option>
                </select>
        <input type="submit" value="Add to Cart" class="buttonStyle" onmouseover="changeOn(this)" onmouseout="changeOff(this)"/>
    </form>
    
    <?php
    if(isset($_SESSION["customerID"])){
    ?>
    <form method="post" name="logout" action="logout.php">
        <input type="submit" value="Logout" class="logoutStyle" onmouseover="changeOn(this)" onmouseout="changeOff(this)"/>
    </form>
    <?php
    }
    ?>
    
</div>	
</div>
<br class="clear" />
</div>
</div>
        <?php        require 'tail.html';
        ?>
        
    </body>
</html>
